package com.zl.pet.service.impl;

import com.zl.basic.service.IBaiduAiAuditService;
import com.zl.basic.service.impl.BaseServiceImpl;
import com.zl.basic.util.LoginContext;
import com.zl.org.domain.Employee;
import com.zl.org.mapper.EmpMapper;
import com.zl.pet.domain.Pet;
import com.zl.pet.domain.PetDetail;
import com.zl.pet.domain.PetOnlineAuditLog;
import com.zl.pet.mapper.PetDetailMapper;
import com.zl.pet.mapper.PetMapper;
import com.zl.pet.mapper.PetOnlineAuditLogMapper;
import com.zl.pet.service.IPetService;
import com.zl.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override
    public void save(Pet pet) {

        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }


    @Transactional
    @Override
    public void remove(Long id) {
        super.remove(id);
        petDetailMapper.delPetDetailByPetId(id);

    }

    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }

    }

    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        LoginInfo loginUser = LoginContext.getLoginUser(request);
         Employee employee =  empMapper.loadByLogininfoId(loginUser.getId());

        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            Boolean textAuditBOO = baiduAiAuditService.textAudit(pet.getName());
            Boolean imageAuditBOO = baiduAiAuditService.imageAudit(pet.getResources());
            if (textAuditBOO &&imageAuditBOO){
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.update(pet);

                //审核日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setNote("审核成功");
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
            }else{

                //审核日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(pet.getId());
                petOnlineAuditLog.setNote("审核失败");
                petOnlineAuditLog.setState(-1);
                petOnlineAuditLog.setAuditTime(new Date());
                petOnlineAuditLog.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
            }


        }

    }

    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();
        petMapper.offsale(ids,offsaletime);
    }
}
